import 'dart:async';
import 'dart:io';

import 'package:flutter/services.dart';
import 'event/ad_event_handler.dart';
export 'event/ad_event_handler.dart';
export 'view/ad_banner_widget.dart';

/// 广告 SDK
class FlutterFnSdk {
  // 方法通道
  static const MethodChannel _methodChannel = const MethodChannel('flutter_fn_sdk');

  // 事件通道
  static const EventChannel _eventChannel = const EventChannel('flutter_fn_sdk_event');

  /// 请求应用跟踪透明度授权
  static Future<bool> get requestIDFA async {
    if (Platform.isIOS) {
      final bool result = await _methodChannel.invokeMethod('requestIDFA');
      return result;
    }
    return true;
  }

  /// 初始化广告
  /// [appId] 应用媒体ID
  /// [test] 是否开启测试
  /// [debug] 是否开启调式
  static Future<bool> initAd(String appId, {bool test = false, bool debug = false}) async {
    final bool result = await _methodChannel.invokeMethod(
      'initAd',
      {
        'appId': appId,
        'test': test,
        'debug': debug,
      },
    );
    return result;
  }

  /// 展示开屏广告
  /// [posId] 广告位 id
  /// [logo] 如果传值则展示底部logo，不传不展示，则全屏展示
  static Future<bool> showSplashAd(String posId, {String? logo}) async {
    final bool result = await _methodChannel.invokeMethod(
      'showSplashAd',
      {
        'posId': posId,
        'logo': logo,
      },
    );
    return result;
  }

  /// 展示插屏广告
  /// [posId] 广告位 id
  static Future<bool> showInterstitialAd(String posId) async {
    final bool result = await _methodChannel.invokeMethod(
      'showInterstitialAd',
      {'posId': posId},
    );
    return result;
  }

  /// 展示激励视频广告
  /// [posId] 广告位 id
  /// [customData] 设置服务端验证的自定义信息
  /// [userId] 设置服务端验证的用户信息
  static Future<bool> showRewardVideoAd(
    String posId, {
    String? customData,
    String? userId,
  }) async {
    final bool result = await _methodChannel.invokeMethod(
      'showRewardVideoAd',
      {
        'posId': posId,
        'customData': customData,
        'userId': userId,
      },
    );
    return result;
  }

  /// 预加载激励视频广告
  /// [posId] 广告位 id
  /// [customData] 设置服务端验证的自定义信息
  /// [userId] 设置服务端验证的用户信息
  static Future<bool> loadOnlyVideoAd(
    String posId, {
    String? customData,
    String? userId,
  }) async {
    final bool result = await _methodChannel.invokeMethod(
      'loadOnlyVideoAd',
      {
        'posId': posId,
        'customData': customData,
        'userId': userId,
      },
    );
    return result;
  }

  /// 仅展示激励视频广告
  static Future<bool> showOnlyVideoAd() async {
    final bool result = await _methodChannel.invokeMethod('showOnlyVideoAd');
    return result;
  }

  /// 展示全屏插屏广告
  /// [posId] 广告位 id
  static Future<bool> showScreenVideoAd(String posId) async {
    final bool result = await _methodChannel.invokeMethod(
      'showScreenVideoAd',
      {'posId': posId},
    );
    return result;
  }

  /// 关闭全屏插屏广告
  /// [posId] 广告位 id
  static Future<bool> closeScreenVideoAd(String posId) async {
    final bool result = await _methodChannel.invokeMethod(
      'closeScreenVideoAd',
      {'posId': posId},
    );
    return result;
  }

  static Future<String> get platformVersion async {
    final String version = await _methodChannel.invokeMethod('getPlatformVersion');
    return version;
  }

  ///事件回调
  ///@params onData 事件回调
  static Future<void> onEventListener(OnAdEventListener onAdEventListener) async {
    _eventChannel.receiveBroadcastStream().listen((data) {
      hanleAdEvent(data, onAdEventListener);
    });
  }
}
